home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4503 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.6 KB

  1. Path: newsroom.hitc.com!usenet
  2. From: psand@eos.hitc.com (G. Patrick Sand)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How to handle error in constructor
  5. Date: 30 Jan 1996 17:12:54 GMT
  6. Organization: Hughes Aircraft (EOSDIS)
  7. Message-ID: <4eljin$pm@newsroom.hitc.com>
  8. References: <DLyyIM.5EG@teslab.lab.oz.au>
  9. NNTP-Posting-Host: 155.157.118.56
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.99.3
  12.  
  13. In article <DLyyIM.5EG@teslab.lab.oz.au>, andrew@teslab.lab.oz.au says...
  14. >
  15. >I'm converting and enhancing a simple program in C to C++.  Without goin
  16. >g
  17. >into too much detail I have a class that represents a file on disk.  The
  18. >contructor opens the file, but what should it do if the file cannot be
  19. >opened?  The C program just calls fopen() tests the return value and 
  20. >if there's an error it prints a message and exits.
  21. >
  22. >In C++ I thought to set a flag in the constructor and have a member func
  23. >tion
  24. >that tests the flag to see if the file was opened correctly.  This seems
  25. >rather inelegant -- I guess exceptions would be the elegant way but seem
  26. >like overkill for such a simple program.  I've looked in several C++ boo
  27. >ks but
  28. >error handling is given scant coverage except for exception handling.
  29. >
  30. >Any suggestions would be greatly appreciated.
  31. >
  32. >-- 
  33. >Andrew Phillips (News/Sys Admin)  andrew@teslab.lab.oz.au  +61 2 287 655
  34. >1
  35. >--------------------------------
  36. >Just a SPOKE, not a SPOKESPERSON
  37.  
  38. Yes, I am dealing with the same issues in constructors on my project.  I 
  39. have been looking into the C++ FAQ (Q 43 amd 44; it was just recently 
  40. posted in four parts) or in their book C++ FAQ (Cline and Lomow, ISBN 
  41. 0-201-58958-3, about US$24).  The book has some "skeletal" examples which 
  42. illustrate various benefits/pitfalls of exception handling...
  43.  
  44. I am vacillating between throwing an exception (discouraged here) and 
  45. doing what you discussed:  creating the object but having a status 
  46. indicator to check before you use it--this approach is called either 
  47. "half-baked" or "zombie" and allows you to clean things up gracefully.  
  48. The exception approach was designed specifically for constructors and 
  49. should--if you aren't tricky--automagically destruct things within 
  50. reason.  I'll post what I decide on and why, and look forward to other 
  51. people's thoughts and examples on the matter...
  52.  
  53. Hope this helps...
  54. -- 
  55. G. Patrick Sand
  56. psand@eos.hitc.com
  57. PatSand@aol.com
  58. (301) 925-0791
  59. "Travel Light But Right..."
  60. Microsoft Network is prohibited from redistributing 
  61. this work in any form, in whole or in part.   License 
  62. to distribute this individual post is available to Microsoft
  63. for $999. Posting without permission constitutes an 
  64. agreement to these terms...gps
  65.  
  66.